package com.itheima.algorithm.binarysearch;

/**
 * @Author: fanYuLe
 * @CreateTime: 2025-03-04  15:00
 * @Description: 二分查找
 */
public class BinarySearch {
    public static int binarySearchBasic(int[] a,int target){
        int i =0; int j = a.length-1;
        while (i<=j){
            int m = (i+j)/2;
            //如果am = target 返回m
            //如果am < target 令 i = m+1
            //target < am 返回j=m-1
            if(a[m]<target){
                i = m+1;
            }else if(target<a[m]){
                j = m-1;
            }else {
                return m;
            }
        }
        return -1;
    }



    public static int binarySearch(int[] a,int target){
        int  i =0;
        int j = a.length-1;
        while (i<=j){
            int m = i+(j-i)/2;
            if(a[m]<target){
                i = m+1;
            }else if(target<a[m]){
                j = m-1;
            }else {
                return m;
            }
        }
        return -1;
    }
}
